/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-10-10 16:25:31
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-11-04 18:56:00
 * @FilePath: \实训二——宏业找房\egg\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const { Controller } = require('egg');
class AgentController extends Controller {
    // 获取经纪人数据
    async getAgent() {
        const { ctx } = this;
        const results = await this.app.mysql.select('agent');
        const { name ,state, pageCode, pageSize } = ctx.request.body;
        let newLists = results;
        if (name) {
            newLists = newLists.filter(v => v.name.includes(name)|| v.tel.includes(name))
        }
        if (state) {
            newLists = newLists.filter(v => v.status.includes(state))
        }
        let total = newLists.length;
        newLists = newLists.slice((pageCode * 1 - 1) * pageSize, pageCode * pageSize)
        if (results) {
            ctx.body = {
                code: 200,
                list: newLists,
                total,
                msg: '获取成功',
                results
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '获取失败'
            }
        }
    }
    // 删除数据
    async delAgent() {
        const { ctx } = this;
        const { id } = ctx.query;
        const result = await this.app.mysql.delete('agent', { id });
        if (result) {
            const results = await this.app.mysql.select('agent');
            ctx.body = {
                code: 200,
                msg: '删除成功',
                list: results
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '删除失败'
            }
        }

    }

    // 添加数据
    async addAgent() {
        const { ctx } = this;
        const { name, tel, company, chamber, secondary,rentHouse,status } = ctx.request.body;
        const result = await this.app.mysql.insert('agent', { name, tel, company, chamber, secondary,rentHouse,status });
        if (result) {
            const results = await this.app.mysql.select('agent');
            ctx.body = {
                code: 200,
                msg: '添加成功',
                list: results
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '添加失败'
            }
        }
    }
    // 编辑
    async editAgent() {
        const { ctx } = this;
        const { name, tel, company, chamber, secondary, rentHouse,status,id } = ctx.request.body;
        const row = {
           name, tel, company, chamber, secondary,rentHouse,status
        };

        const options = {
            where: {
                id
            }
        };
        const result = await this.app.mysql.update('agent', row, options);
        if (result) {
            ctx.body = {
                code: 200,
                msg: '修改成功',
                list: result
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '修改失败',
            }
        }
    }
    // 启用
    async able(){
        const {ctx}=this;
        const { status,id }=ctx.request.body;
        if (status == '启用') {
            const row = {
                status: '在职'
            };

            const options = {
                where: {
                    id,
                }
            };
            const result = await this.app.mysql.update('agent', row, options);
            if (result) {
                ctx.body = {
                    code: 200,
                    msg: '启用成功'
                }
            }
        }
    }
    // 禁用
    async enable() {
        const { ctx } = this;
        const { status, id } = ctx.request.body;

        if (status == '禁用') {
            const row = {
                status: '离职'
            };

            const options = {
                where: {
                    id,
                }
            };
            const result = await this.app.mysql.update('agent', row, options);
            if (result) {
                ctx.body = {
                    code: 200,
                    msg: '禁用成功'
                }
            }
        }
    }

   
}

module.exports =AgentController;
